#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;
using tl = tuple < int, int, int >;

const int N = 3e5 + 10;

struct node { int ls, rs, dis; ll val; } t[N << 1];
int p[N], c[N], n, m, deg[N], rt[N], ndn;
ll tot;

int merge(int x, int y) {
	if(!x || !y) return x | y;
	if(t[x].val < t[y].val) swap(x, y);
	t[x].rs = merge(t[x].rs, y);
	if(t[t[x].ls].dis < t[t[x].rs].dis) swap(t[x].ls, t[x].rs);
	t[x].dis = t[t[x].rs].dis + 1; return x;
}

int pop(const int &x) { return merge(t[x].ls, t[x].rs); }

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in;
	rep(i, 2, n + m) p[i] = in, c[i] = in, tot += c[i], deg[p[i]]++;
	per(i, n + m, 2) {
		ll l = 0, r = 0;
		if(deg[i]) {
			while(deg[i] > 1) rt[i] = pop(rt[i]), deg[i]--;
			r = t[rt[i]].val; rt[i] = pop(rt[i]);
			l = t[rt[i]].val; rt[i] = pop(rt[i]);
		}
		t[++ndn].val = l + c[i]; t[++ndn].val = r + c[i];
		rt[i] = merge(rt[i], merge(ndn - 1, ndn));
		rt[p[i]] = merge(rt[p[i]], rt[i]);
	}
	while(deg[1]) rt[1] = pop(rt[1]), deg[1]--;
	while(rt[1]) tot -= t[rt[1]].val, rt[1] = pop(rt[1]);
	cout << tot << endl;
	return 0;
}
